package com.kabuda.blog.controller;

import com.kabuda.blog.dao.pojo.Category;
import com.kabuda.blog.service.IArticleService;
import com.kabuda.blog.service.ICategoryService;
import com.kabuda.blog.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author kabuda
 * @create 2023-04-03 12:36
 */
@Api(tags = "分类接口")
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IArticleService articleService;

    @ApiOperation("分类页src显示图片")
    @GetMapping("/down")
    public Result down(@RequestParam("fileName") String fileName, HttpServletResponse response){
        categoryService.down(fileName,response);
        return Result.success(null);
    }
    @ApiOperation("上传图片")
    @PostMapping("/up")
    public Result up(MultipartFile file){
        String fileName=categoryService.up(file);
        return Result.success(fileName);
    }

    @ApiOperation("获取分类列表")
    @GetMapping("/list")
    public Result list(){
        return Result.success(categoryService.list());
    }
    @ApiOperation("更新分类信息")
    @PostMapping("/edit")
    public Result edit(@RequestBody Category Category){
        boolean b = categoryService.updateById(Category);
        return Result.success(b);
    }
    @ApiOperation("根据id查询Category信息")
    @GetMapping("/getById")
    public Result getById(Long id){
        Category Category = categoryService.getById(id);
        return Result.success(Category);
    }
    @ApiOperation("新增分类信息")
    @PostMapping("/add")
    public Result add(@RequestBody Category Category){
        boolean b = categoryService.save(Category);
        return Result.success(b);
    }
    @ApiOperation("删除分类信息")
    @GetMapping("/delete")
    public Result delete(Long id){
        boolean b = categoryService.removeById(id);
        return Result.success(b);
    }

    @ApiOperation("查询该分类是否有文章关联")
    @GetMapping("/count")
    public Result count(@RequestParam("id") Long categoryId){
        int count = articleService.ArticleByCategoryCount(categoryId);
        return Result.success(count);
    }

    @ApiOperation("删除分类关联信息（修改文章分类为默认分类）")
    @GetMapping("/deleteRelation")
    public Result deleteRelation(@RequestParam("id") Long categoryId){
        categoryService.deleteRelation(categoryId);
        return Result.success(null);
    }


}
